package com.sukumar.android.backit.mail;

import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class Mail extends javax.mail.Authenticator {
	private String _user;
	private String _pass;

	private String[] _to;
	private String _from;

	private String _subject;
	private String _body;

	private boolean _auth;

	private boolean _debuggable;

	Properties _props = null;

	public Mail() {
		_user = "";
		_pass = "";
		_from = "";
		_subject = "";
		_body = "";

		_debuggable = false;
		_auth = true;
	}

	public Mail(String user, String pass, String provider) {
		this();
		if (provider.equals("GM")) {
			_props = setPropsGmail();
		} else if (provider.equals("YH")) {
			_props = setPropsYahoo();
		} else if (provider.equals("HM")) {
			_props = setPropsHotmail();
		}
		_user = user;
		_pass = pass;
	}

	public boolean send() throws Exception {
		if (!_user.equals("") && !_pass.equals("") && _to.length > 0
				&& !_from.equals("") && !_subject.equals("")) {

			Session session = Session.getInstance(_props, this);
			session.setDebug(_debuggable);

			Message msg = new MimeMessage(session);
			msg.setFrom(new InternetAddress(_from));
			InternetAddress[] addressTo = new InternetAddress[_to.length];
			for (int i = 0; i < _to.length; i++) {
				addressTo[i] = new InternetAddress(_to[i]);
			}
			msg.setRecipients(MimeMessage.RecipientType.TO, addressTo);
			msg.setSubject(_subject);
			msg.setSentDate(new Date());
			msg.setText("");

			Transport.send(msg);

			return true;
		} else {
			return false;
		}
	}

	@Override
	public PasswordAuthentication getPasswordAuthentication() {
		return new PasswordAuthentication(_user, _pass);
	}

	private Properties setPropsGmail() {
		Properties props = new Properties();
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.socketFactory.port", "465");
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.port", "465");
		props.put("mail.smtp.socketFactory.class",
				"javax.net.ssl.SSLSocketFactory");
		return props;
	}

	private Properties setPropsYahoo() {
		Properties props = new Properties();
		props.put("mail.smtp.host", "smtp.mail.yahoo.com");
		props.put("mail.smtp.socketFactory.port", "465");
		props.put("mail.smtp.socketFactory.class",
				"javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.port", "587");
		return props;
	}

	private Properties setPropsHotmail() {
		Properties props = new Properties();
		props.put("mail.smtp.host", "smtp.live.com");
		props.put("mail.smtp.starttls.enable", "true");
		props.put("mail.smtp.socketFactory.port", "587");
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.port", "587");
		return props;
	}

	public String get_user() {
		return _user;
	}

	public void set_user(String _user) {
		this._user = _user;
	}

	public String get_pass() {
		return _pass;
	}

	public void set_pass(String _pass) {
		this._pass = _pass;
	}

	public String[] get_to() {
		return _to;
	}

	public void set_to(String[] _to) {
		this._to = _to;
	}

	public String get_from() {
		return _from;
	}

	public void set_from(String _from) {
		this._from = _from;
	}

	public String get_subject() {
		return _subject;
	}

	public void set_subject(String _subject) {
		this._subject = _subject;
	}

	public String get_body() {
		return _body;
	}

	public void set_body(String _body) {
		this._body = _body;
	}

	public boolean is_auth() {
		return _auth;
	}

	public void set_auth(boolean _auth) {
		this._auth = _auth;
	}

	public boolean is_debuggable() {
		return _debuggable;
	}

	public void set_debuggable(boolean _debuggable) {
		this._debuggable = _debuggable;
	}

}
